<template>
  <view class="main">
    <view class="cover">
      <image src="/static/ls.png"></image>
    </view>
    <button class="btn" @click="userLogin">一键登录</button>
  </view>
</template>

<script setup>
import {ref, reactive} from 'vue';
import {login, getUserInfo} from '@/api/apiList';


let formData = reactive({
  wxCode: ''
})
const userLogin = async (e) => {
  await uni.showLoading({
    mask: true,
    title: '登录中',
	duration:2000
  });
  await uni.login({
    success: async (result) => {
      formData.wxCode = result.code
      const res = await login(formData);
      if (res.code == 200) {
        uni.setStorageSync("token", res.msg)
        await getUser()
      }
    }
  });

};

const userInfo = ref({})
const getUser = async () => {
  const res = await getUserInfo()
  if (res.code == 200) {
    userInfo.value = res.data
    uni.hideLoading()
    if (res.data.phonenumber && res.data.status == 0) {
      uni.setStorageSync('userInfo', userInfo.value)
      await uni.showToast({
        title: '登录成功',
        icon: 'none'
      })
      setTimeout(() => {
        uni.reLaunch({
          url: '/pages/index/index'
        })
      }, 1500)
    } else if (res.data.status == 2) {
      await uni.showToast({
        title: '账号待审核',
        icon: 'none'
      })
    } else {
      await uni.showToast({
        title: '未注册,前往注册',
        icon: 'none'
      })
      setTimeout(() => {
        uni.redirectTo({
          url: '/subPackage/mine/register'
        })
      }, 1500)
    }
  }
}

</script>

<style>
page {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  background-color: #ffbf0e;
}
</style>

<style lang="scss" scoped>
.cover {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 100rpx;

  image {
    width: 160rpx;
    height: 160rpx;
  }
}

.btn {
  width: 686rpx;
  height: 88rpx;
  background: #ffd257;
  border-radius: 44rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #000;
}

.cancel{
	margin-top: 60rpx;
}
</style>
